package Test;

public class Test3 {
    volatile static int count = 0;
   static Object object = new Object();//锁对象
    volatile static int size = 1;
    public static void main(String[] args) throws InterruptedException {
        Thread A = new Thread(()->{
            while (count < 30){

                synchronized (object){
                    //唤醒其余两个线程
                    object.notifyAll();

                    if(count % 3 == 0) {
                        System.out.print(Thread.currentThread().getName());
                        //当我打印之后就不需要占有这把锁就可以wait了
                        count++;
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        },"A");
        Thread B = new Thread(()->{
            while (count < 30){
                synchronized (object){
                    //唤醒其余两个线程
                    object.notifyAll();

                    if(count % 3 == 1){
                        System.out.print(Thread.currentThread().getName());
                        //当我打印之后就不需要占有这把锁就可以wait了
                        count++;
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        },"B");
        Thread C = new Thread(()->{
            while (count < 30){
                synchronized (object){
                    //唤醒其余两个线程
                    object.notifyAll();

                    if(count % 3 == 2) {
                        System.out.println(Thread.currentThread().getName()+size++);
                        //当我打印之后就不需要占有这把锁就可以wait了
                        count++;
                        try {
                            //当count到30的时候就不需要在wait了
                            if(count != 30)
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        },"C");
        //按顺序打印abc
        //可以采用定时器嘛,定时器是输入指定时间,当该时间到的时候才执行,
        A.start();
        B.start();
        C.start();
        A.join();
        B.join();
        C.join();

    }

}
